﻿using Microsoft.Office.InfoPath.Server.Administration;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Publishing.Administration;

namespace SharePoint.Security.ConfigAnalyzer.Engine.Common
{
    public static class ObjectHelper
    {
        public static void PreFetch()
        {
            var time = new System.Diagnostics.Stopwatch();
            time.Start();
            var farm = Farm;
            var av = AvSettings;
            var cd = CDConfiguration;
            var dia = Diagnostics;
            var um = UsageManager;
            var fs =FormsService;
            var sp = SPAdministrationWeb;
            TimeTaken = time.Elapsed.Seconds;
            time.Stop();
        }

        public static int TimeTaken 
        { 
            get; 
            set; 
        }

        private static SPFarm _farm;
        public static SPFarm Farm
        {
            get
            {
                return _farm ?? (_farm = SPFarm.Local);
            }
        }

        private static SPAntivirusSettings _avSettings;
        public static SPAntivirusSettings AvSettings
        {
            get
            {
                return _avSettings ?? (_avSettings = Farm.Services.GetValue<SPWebService>().AntivirusSettings);
            }
        }

        private static ContentDeploymentConfiguration _cdConfiguration;
        public static ContentDeploymentConfiguration CDConfiguration
        {
            get
            {
                return _cdConfiguration ?? (_cdConfiguration = ContentDeploymentConfiguration.GetInstance());
            }
        }

        private static SPDiagnosticsService _diagnostics;
        public static SPDiagnosticsService Diagnostics
        {
            get
            {
                return _diagnostics ?? (_diagnostics = SPDiagnosticsService.Local);
            }
        }

        private static SPUsageManager _usageManager;
        public static SPUsageManager UsageManager
        {
            get
            {
                return _usageManager ?? (_usageManager = SPUsageManager.Local);
            }
        }

        private static FormsService _formsService;
        public static FormsService FormsService
        {
            get
            {
                return _formsService ?? (_formsService = Farm.Services.GetValue<FormsService>(FormsService.ServiceName));
            }
        }

        private static SPAdministrationWebApplication _spAdministrationWeb;
        public static SPAdministrationWebApplication SPAdministrationWeb
        {
            get
            {
                return _spAdministrationWeb ?? (_spAdministrationWeb = SPAdministrationWebApplication.Local);
            }
        }
    }
}
